<template>
  <div class="production-task-page">
    <el-card class="task-card">
      <template #header>
        <div class="card-header">
          <span>生产任务管理</span>
          <el-button type="primary" @click="handleAdd" v-if="hasPerm('production:task:add')">新增任务</el-button>
        </div>
      </template>
      
      <el-table :data="taskList" style="width: 100%">
        <el-table-column prop="taskCode" label="任务编号" width="120" />
        <el-table-column prop="productName" label="产品名称" width="150" />
        <el-table-column prop="quantity" label="生产数量" width="100" />
        <el-table-column prop="status" label="状态" width="100">
          <template #default="scope">
            <el-tag :type="getStatusType(scope.row.status)">
              {{ getStatusText(scope.row.status) }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="startDate" label="开始日期" width="120" />
        <el-table-column prop="endDate" label="完成日期" width="120" />
        <el-table-column label="操作" width="200">
          <template #default="scope">
            <el-button size="small" @click="handleView(scope.row)" v-if="hasPerm('production:task:view')">查看</el-button>
            <el-button size="small" type="primary" @click="handleEdit(scope.row)" v-if="hasPerm('production:task:edit') && scope.row.status === 'unaudited'">编辑</el-button>
            <el-button size="small" type="success" @click="handleProcess(scope.row)" v-if="hasPerm('production:task:process') && scope.row.status === 'processing'">加工</el-button>
            <el-button size="small" type="danger" @click="handleDelete(scope.row)" v-if="hasPerm('production:task:delete')">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { hasPerm } from '@/utils/permission'
import { ElMessage } from 'element-plus'

const taskList = ref([])

const getStatusType = (status) => {
  const statusMap = {
    'unaudited': 'warning',
    'processing': 'primary',
    'completed': 'success'
  }
  return statusMap[status] || 'info'
}

const getStatusText = (status) => {
  const statusMap = {
    'unaudited': '未审核',
    'processing': '加工中',
    'completed': '已完工'
  }
  return statusMap[status] || '未知'
}

const handleAdd = () => {
  ElMessage.info('新增生产任务功能')
}

const handleView = (row) => {
  ElMessage.info(`查看任务：${row.taskCode}`)
}

const handleEdit = (row) => {
  ElMessage.info(`编辑任务：${row.taskCode}`)
}

const handleProcess = (row) => {
  ElMessage.info(`加工操作：${row.taskCode}`)
}

const handleDelete = (row) => {
  ElMessage.info(`删除任务：${row.taskCode}`)
}

onMounted(() => {
  // 模拟数据
  taskList.value = [
    {
      taskCode: 'TASK001',
      productName: '产品A',
      quantity: 100,
      status: 'unaudited',
      startDate: '2025-07-30',
      endDate: '2025-08-05'
    },
    {
      taskCode: 'TASK002',
      productName: '产品B',
      quantity: 50,
      status: 'processing',
      startDate: '2025-07-29',
      endDate: '2025-08-03'
    },
    {
      taskCode: 'TASK003',
      productName: '产品C',
      quantity: 200,
      status: 'completed',
      startDate: '2025-07-25',
      endDate: '2025-07-28'
    }
  ]
})
</script>

<style scoped>
.production-task-page {
  padding: 20px;
}

.task-card {
  margin-bottom: 20px;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
</style> 